package com.situ.web.servlet;

import com.situ.web.pojo.Banji;
import com.situ.web.service.IBanjiService;
import com.situ.web.service.impl.BanjiServiceImpl;
import com.situ.web.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/banji")
public class BanjiServlet extends HttpServlet {
    //private IBanjiDao banjiDao = new BanjiDaoImpl();
    private IBanjiService banjiService = new BanjiServiceImpl();

    //重写service方法,访问servlet默认访问service方法
    // http://localhost:8080/hello.jsp
    // http://localhost:8080/banji
    // http://localhost:8080/banji?method=selectAll
    // http://localhost:8080/banji?method=deleteById&id=2
    // http://localhost:8080/banji?method=add
    // http://localhost:8080/banji?method=delete
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决post请求乱码问题
        //req.setCharacterEncoding("UTF-8");
        /*HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            resp.sendRedirect("/login.jsp");
            return;
        }*/

        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "selectAll":
                selectAll(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "toUpdate":
                toUpdate(req, resp);
                break;
            case "update":
                update(req, resp);
                break;

        }
    }

    // /banji?method=selectByPage&pageNo=1&pageSize=5
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");
        if (pageNo == null || pageNo.equals("")) {
            pageNo = "1";
        }
        if (pageSize == null || pageSize.equals("")) {
            pageSize = "5";
        }
        PageInfo pageInfo = banjiService.selectByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        System.out.println(pageInfo);
        req.setAttribute("pageInfo", pageInfo);
        req.getRequestDispatcher("/banji_list.jsp").forward(req, resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BanjiServlet.update");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        Banji banji = new Banji(Integer.parseInt(id), name, address);
        banjiService.update(banji);
        resp.sendRedirect("/banji");
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("BanjiServlet.toUpdate");
        String id = req.getParameter("id");
        Banji banji = banjiService.selectById(Integer.parseInt(id));
        req.setAttribute("banji", banji);
        req.getRequestDispatcher("/banji_update.jsp").forward(req, resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BanjiServlet.add");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        Banji banji = new Banji(name, address);
        banjiService.add(banji);
        resp.sendRedirect("/banji");
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BanjiServlet.deleteById");
        String id = req.getParameter("id");
        banjiService.deleteById(Integer.parseInt(id));
        //重定向到/banji查找所有这个servlet
        //重定向状态码： 302
        resp.sendRedirect("/banji");
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("BanjiServlet.selectAll");
        List<Banji> list = banjiService.selectAll();
        //把list放到request里面，理解成放到一块内存区域里面
        req.setAttribute("list", list);
        //跳转到banji_list.jsp进行展示
        req.getRequestDispatcher("/banji_list.jsp").forward(req, resp);
    }
}
